package com.evil.application.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * 编号类型枚举
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum GenerateSnEnum {

    /**
     * 编号类型枚举
     * 这里加枚举记得去数据库加
     */
    USER_CODE("USER_CODE", "用户工号"),
    APPLICATION_ORDER_SN("APPLICATION_ORDER_SN", "应用订单编号"),
    ;

    @Getter
    private final String type;
    @Getter
    private final String name;

    public static Optional<GenerateSnEnum> findByType(String type) {
        if (StringUtils.isBlank(type)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type.equals(type)).findFirst();
    }

    public static GenerateSnEnum isExistByType(String type) {
        return GenerateSnEnum.findByType(type).orElseThrow(()
                -> new BusinessException(RCodeEnum.ERROR_PARAMETER, String.format("编号类型[%s]不存在", type)));
    }
}
